<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

class UserDraw extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id','activity_id','customer_info_id','draw_at','prize','address','address_at','reason'
    ];

    protected $casts = [
        'draw_at' => 'datetime',
        'address_at' => 'datetime',
        'prize' => 'json',
        'address' => 'json'
    ];

    public function addRecond($activity_id, $user_id, $customer_info_id)
    {
        $data = [
            'activity_id'=>$activity_id,
            'user_id'=>$user_id,
            'customer_info_id'=>$customer_info_id,
        ];
        $result = $this->create($data);
        return $result;
    }

    public function activity()
    {
        return $this->belongsTo(Activity::class);
    }

    public function scopeWin($query, $win)
    {
        if ($win) {
            return $query->whereNotNull('prize');
        } else {
            return $query->whereNull('prize');
        }
    }

    public function customerInfo()
    {
        return $this->belongsTo(CustomerInfo::class);
    }
}
